package com.yuanduan.mplus.view;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.cj.framework.app.BaseActivity;
import com.cj.framework.util.HttpImp;
import com.cj.framework.util.IHttp;
import com.cj.framework.util.RSAUtils;
import com.cj.framework.widget.HorizontalBarChart;
import com.yuanduan.mplus.R;
import com.yuanduan.mplus.config.Config;
import com.yuanduan.mplus.model.DBManager;
import com.yuanduan.mplus.model.entity.User;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;

/**
 * 废弃
 */
public class EconomicalDriveDetailActivity extends BaseActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {
    private ArrayList<SafetyDriveDetailActivity.DateInfo>  dateInfos;
    private Calendar calendar=Calendar.getInstance();
    private HorizontalBarChart hcb_0;
    private HorizontalBarChart hcb_1;
    private HorizontalBarChart hcb_2;
    private HorizontalBarChart hcb_3;
    private LinearLayout ll_1;
    private LinearLayout ll_2;
    private LinearLayout ll_3;
    private LinearLayout ll_4;
    private TextView tv_title;
    private ImageView tv_back;
    private TextView tv_lookRecord;
    private RadioGroup rg;
    private User user;

    private HorizontalBarChart[] charts;
    private ProgressDialog progressDialog;
    private int[][] data=new int[4][3];
    private IHttp iHttp=new HttpImp();
    private IHttp.HttpInfoCallbackWithIndex callback=new IHttp.HttpInfoCallbackWithIndex() {
        @Override
        public void onSucceed(Object result, int index) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    progressDialog.dismiss();
                }
            },500);
            String result1= RSAUtils.decryptWithRSA((String) result);
            JSONObject jsonObject=null;
            try {
                jsonObject=new JSONObject(result1);
                if(jsonObject==null){
                    return;
                }
                if(jsonObject.getInt("RS")!=1){
                    return;
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            switch (index){
                case 0:
                    fillData(jsonObject);
                    animalBar();
                    break;
                case 1:
                    fillData(jsonObject);
                    animalBar();
                    break;
                case 2:
                    fillData(jsonObject);
                    animalBar();
                    break;
            }
        }

        @Override
        public void onFailed(Object result, int index) {

        }
    };
    @Override
    protected void findId() {
        hcb_0= (HorizontalBarChart) findViewById(R.id.ehbc_1);
        hcb_1= (HorizontalBarChart) findViewById(R.id.ehbc_2);
        hcb_2= (HorizontalBarChart) findViewById(R.id.ehbc_3);
        hcb_3= (HorizontalBarChart) findViewById(R.id.ehbc_4);
        ll_1= (LinearLayout) findViewById(R.id.economicaldetail_ll_1);
        ll_2= (LinearLayout) findViewById(R.id.economicaldetail_ll_2);
        ll_3= (LinearLayout) findViewById(R.id.economicaldetail_ll_3);
        ll_4= (LinearLayout) findViewById(R.id.economicaldetail_ll_4);
        rg= (RadioGroup) findViewById(R.id.economicaldetail_rg);
        tv_title= (TextView) findViewById(R.id.economicaldetail_title);
        tv_back= (ImageView) findViewById(R.id.economicaldetail_back);
        tv_lookRecord= (TextView) findViewById(R.id.economicaldetail_lookrecord);
        charts=new HorizontalBarChart[]{hcb_0,hcb_1,hcb_2,hcb_3};
    }

    @Override
    protected void logic() {
        user=DBManager.getInstance(getApplicationContext()).getUser();
        dateInfos=SafetyDriveDetailActivity.getDateInfos();
        ((RadioButton)rg.getChildAt(0)).setText(dateInfos.get(0).weekcode+"周(上周)");
        ((RadioButton)rg.getChildAt(1)).setText(dateInfos.get(1).weekcode+"周");
        ((RadioButton)rg.getChildAt(2)).setText(dateInfos.get(2).weekcode+"周");
        ((RadioButton)rg.getChildAt(3)).setText(dateInfos.get(3).weekcode+"周");
        ll_1.setOnClickListener(this);
        ll_2.setOnClickListener(this);
        ll_3.setOnClickListener(this);
        ll_4.setOnClickListener(this);
        rg.setOnCheckedChangeListener(this);
        tv_back.setOnClickListener(this);
        tv_lookRecord.setOnClickListener(this);
        progressDialog=new ProgressDialog(this);
        rg.check(R.id.economicaldetail_rb_zhou1);
        if(Config.userType==1){
            //showprogress
            //发送网络请求
            progressDialog.show();
            HashMap<String,String> hashMap1=new HashMap<>();
            hashMap1.put("UserPhone", user.getUserPhone());
            hashMap1.put("YearCode",dateInfos.get(0).yearcode+"");
            hashMap1.put("WeekCode",dateInfos.get(0).weekcode+"");
            iHttp.httpInfoPostWithIndex(Config.URL+Config.USERECONOMIC,Config.translateMap(hashMap1),callback,0);
        }else if(Config.userType==2){
            //设置title
            //showprogress
            //发送网络请求
            Bundle bundle=getIntent().getExtras();
            tv_title.setText(bundle.getString("RealName")+"经济驾驶详情");
            progressDialog.show();
            HashMap<String,String> hashMap1=new HashMap<>();
            hashMap1.put("UserPhone",bundle.getString("UserPhone"));
            hashMap1.put("YearCode",dateInfos.get(0).yearcode+"");
            hashMap1.put("WeekCode",dateInfos.get(0).weekcode+"");
            iHttp.httpInfoPostWithIndex(Config.URL+Config.USERECONOMIC,Config.translateMap(hashMap1),callback,0);
        }else{
            //游客
        }
    }

    @Override
    protected int setLayout() {
        return R.layout.activity_economical_drive_detail;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.economicaldetail_back:
                finish();
                break;
            case R.id.economicaldetail_lookrecord:
                openActivity(DriveRecordActivity.class);
                break;
            case R.id.economicaldetail_ll_1:
                Intent intent=new Intent(this,ReportActivity.class);
                intent.putExtra("reportType","加速预警");
                intent.putExtra("Type","ACC");
                intent.putExtra("Choose","economic");
                if(Config.userType==2){
                    Bundle bundle=new Bundle();
                    bundle.putString("UserPhone",getIntent().getExtras().getString("UserPhone"));
                    bundle.putString("RealName",getIntent().getExtras().getString("RealName"));
                    intent.putExtras(bundle);
                }
                startActivity(intent);
                break;
            case R.id.economicaldetail_ll_2:
                Intent intent1=new Intent(this,ReportActivity.class);
                intent1.putExtra("reportType","车距预警");
                intent1.putExtra("Type","HMW");
                intent1.putExtra("Choose","economic");
                if(Config.userType==2){
                    Bundle bundle=new Bundle();
                    bundle.putString("UserPhone",getIntent().getExtras().getString("UserPhone"));
                    bundle.putString("RealName",getIntent().getExtras().getString("RealName"));
                    intent1.putExtras(bundle);
                }
                startActivity(intent1);
                break;
            case R.id.economicaldetail_ll_3:
                Intent intent2=new Intent(this,ReportActivity.class);
                intent2.putExtra("reportType","急刹预警");
                intent2.putExtra("Type","BRAKE");
                intent2.putExtra("Choose","economic");
                if(Config.userType==2){
                    Bundle bundle=new Bundle();
                    bundle.putString("UserPhone",getIntent().getExtras().getString("UserPhone"));
                    bundle.putString("RealName",getIntent().getExtras().getString("RealName"));
                    intent2.putExtras(bundle);
                }
                startActivity(intent2);
                break;
            case R.id.economicaldetail_ll_4:
                Intent intent3=new Intent(this,ReportActivity.class);
                intent3.putExtra("reportType","转弯加速");
                intent3.putExtra("Type","TA");
                intent3.putExtra("Choose","economic");
                if(Config.userType==2){
                    Bundle bundle=new Bundle();
                    bundle.putString("UserPhone",getIntent().getExtras().getString("UserPhone"));
                    bundle.putString("RealName",getIntent().getExtras().getString("RealName"));
                    intent3.putExtras(bundle);
                }
                startActivity(intent3);
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        for(int i=0;i<charts.length;i++){
            charts[i].setLength(0,0,0);
        }
        HashMap<String,String> hashMap=new HashMap<>();
        if(Config.userType==1){
            hashMap.put("UserPhone",user.getUserPhone());
        }else if(Config.userType==2){
            hashMap.put("UserPhone",getIntent().getExtras().getString("UserPhone"));
        }
            switch (checkedId){
                case R.id.economicaldetail_rb_zhou1:
                    progressDialog.show();
                    hashMap.put("YearCode",dateInfos.get(0).yearcode+"");
                    hashMap.put("WeekCode",dateInfos.get(0).weekcode+"");
                    iHttp.httpInfoPostWithIndex(Config.URL+Config.USERECONOMIC,Config.translateMap(hashMap),callback,0);
                    break;
                case R.id.economicaldetail_rb_zhou2:
                    progressDialog.show();
                    hashMap.put("YearCode",dateInfos.get(1).yearcode+"");
                    hashMap.put("WeekCode",dateInfos.get(1).weekcode+"");
                    iHttp.httpInfoPostWithIndex(Config.URL+Config.USERECONOMIC,Config.translateMap(hashMap),callback,1);
                    break;
                case R.id.economicaldetail_rb_zhou3:
                    progressDialog.show();
                    hashMap.put("YearCode",dateInfos.get(2).yearcode+"");
                    hashMap.put("WeekCode",dateInfos.get(2).weekcode+"");
                    iHttp.httpInfoPostWithIndex(Config.URL+Config.USERECONOMIC,Config.translateMap(hashMap),callback,2);
                    break;
                case R.id.economicaldetail_rb_zhou4:
                    progressDialog.show();
                    hashMap.put("YearCode",dateInfos.get(3).yearcode+"");
                    hashMap.put("WeekCode",dateInfos.get(3).weekcode+"");
                    iHttp.httpInfoPostWithIndex(Config.URL+Config.USERECONOMIC,Config.translateMap(hashMap),callback,2);
                    break;
            }
    }

    private void fillData(JSONObject result){
        if(result==null){
            return;
        }
        try {
            JSONObject acc=result.getJSONObject("ACC");
            JSONObject hmw=result.getJSONObject("HMW");
            JSONObject brake=result.getJSONObject("BRAKE");
            JSONObject ta=result.getJSONObject("TA");
            JSONObject se=result.getJSONObject("SE");
            data[0][0]=acc.getInt("self");
            data[0][1]=acc.getInt("average");
            data[0][2]=acc.getInt("max");
            data[1][0]=acc.getInt("self");
            data[1][1]=acc.getInt("average");
            data[1][2]=acc.getInt("max");
            data[2][0]=acc.getInt("self");
            data[2][1]=acc.getInt("average");
            data[2][2]=acc.getInt("max");
            data[3][0]=acc.getInt("self");
            data[3][1]=acc.getInt("average");
            data[3][2]=acc.getInt("max");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void animalBar(){
        int[] temp=new int[3];
        for(int i=0;i<data.length;i++){
            for(int j=0;j<data[i].length;j++){
                temp[j]=data[i][j];
            }
            charts[i].setLength(temp[0],temp[1],temp[2]);
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        animalBar();
    }
}
